using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameVuTru.ManagerScreens;
using GameVuTru.ResourceManager;
using GameVuTru.Screens;
namespace GameVuTru.GameObject
{
    public class Player : CObject
    {
        public List<Bullet> Listbullet;

        public static BULLETTYPE bullettype = BULLETTYPE.BULLETBLUE;
        public static int PowerBullet = 1;
        public static int StrenghtBullet = 2;
        public List<Explore> listexplore;
        //Ban boom ra
        public List<CBoom> ListBoom;
        public bool checkboom;
        //public bool checkexplosionboom;//bao hieu vu no cua trai boom

        //bao ve nguoi choi ne
        public bool Isprotected;//duoc bao ve hay khong
        public static float TimeProtected = 10;//thoi gian duoc bao ve
        public float TimeCountProtected;//thoi gian tinh de het bao ve
        public CircleProtected cirprotect;
        public Player()
        {
            postion = new Vector2(380, 400);
            Speed = 8;
            IsCollision = false;
            listexplore = new List<Explore>();
            Listbullet = new List<Bullet>();
            ListBoom = new List<CBoom>();
            checkboom = false;
            Isprotected = false;
            TimeCountProtected = 0;
            m_sprite = new CSprite(ReSouce.phithuyen, 3, 3, 180, 60);
            cirprotect = new CircleProtected();
        }
        public override void Update(GameTime gametime, List<CObject> listobject)
        {
            //tinh thoi gian nao va lam bao ve nguoi choi
            cirprotect.postion = postion;
            if (Isprotected)
            {

                TimeCountProtected += (float)gametime.ElapsedGameTime.TotalSeconds;
            }
            if (TimeCountProtected >= TimeProtected)
            {
                Isprotected = false;
            }
            if (Isprotected)
            {
                cirprotect.Update(gametime);
            }
            /*****************************************************************************************************/
            m_sprite.UpdateSprite(gametime, 0, 2, 1);

            //keep player in screen
            if (postion.X <= 0) postion.X = 0;
            if (postion.X + m_sprite.widthsprite >= 800) postion.X = 800 - m_sprite.widthsprite;
            if (postion.Y <= 0) postion.Y = 0;
            if (postion.Y >= 480 - m_sprite.heightsprite) postion.Y = 480 - m_sprite.heightsprite;

            //xoa cac bullet ko kon ve
            if (Listbullet.Count > 0)
            {
                for (int i = 0; i < Listbullet.Count; i++)
                {
                    if (!Listbullet[i].Isvisible)
                    {
                        Listbullet.RemoveAt(i);
                    }
                }
            }
            //ban boom ra ne
            if (checkboom)
            {
                checkboom = false;
                ListBoom.Add(new CBoom(postion));
            }
            //boom no ne
            //if (checkexplosionboom)
            //{
            //    checkboom = false;
            //    listexplore.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION,new Vector2(400,240)));
            //}
            //xoa may cai isvisible = false roi ra khoi danh sach
            RemoveAllList();
            // update bullet
            foreach (Bullet item in Listbullet)
            {
                item.Update(gametime);
            }

            foreach (Explore item in listexplore)
            {
                item.Update(gametime);
            }
            foreach (CBoom item in ListBoom)
            {
                item.Update(gametime);
            }


            base.Update(gametime, listobject);
        }
        public override void Draw(SpriteBatch sp)
        {


            foreach (Bullet item in Listbullet)
            {
                item.Draw(sp);

            }
            foreach (Explore item in listexplore)
            {
                item.Draw(sp);

            }
            foreach (CBoom item in ListBoom)
            {
                item.Draw(sp);

            }
            if (Isprotected)
            {
                cirprotect.Draw(sp);
            }
            base.Draw(sp);

        }
        public override void UpdateCollion(CObject b)
        {
            if (isvisible)
            {
                if (b.typeobject == OBJECTTYPE.BONUS)
                {
                    ItemBonus bn = (ItemBonus)b;
                    if (this.bound.Intersects(b.bound) && b.isvisible)
                    {
                        //  b.isvisible = false;
                        bn.isvisible = false;

                        switch (bn.bonustype)
                        {
                            case BONUSTYPE.THREECOLORBOX:
                                PowerBullet++;
                                GameplayScreen.numScore += 500;
                                ManagerSound.PlaySound(ReSouce.soundEatBullet);
                                break;
                            case BONUSTYPE.REDBOX:
                                GameplayScreen.numScore += 500;
                                ManagerSound.PlaySound(ReSouce.soundEatBullet);
                                if (bullettype == BULLETTYPE.BULLETBLUE)
                                {
                                    PowerBullet++;

                                }
                                else
                                {
                                    bullettype = BULLETTYPE.BULLETBLUE;
                                }
                                break;
                            case BONUSTYPE.YELLOWBOX:
                                GameplayScreen.numScore += 500;
                                ManagerSound.PlaySound(ReSouce.soundEatBullet);
                                if (bullettype == BULLETTYPE.BULLETYELLOW)
                                {
                                    PowerBullet++;
                                }
                                else
                                {
                                    bullettype = BULLETTYPE.BULLETYELLOW;
                                }
                                break;
                            case BONUSTYPE.BLUEBOX:
                                GameplayScreen.numScore += 500;
                                ManagerSound.PlaySound(ReSouce.soundEatBullet);
                                if (bullettype == BULLETTYPE.BULLETRED)
                                {
                                    PowerBullet++;
                                }
                                else
                                {
                                    bullettype = BULLETTYPE.BULLETRED;
                                }
                                break;
                            case BONUSTYPE.SHIELD:
                                GameplayScreen.numScore += 500;
                                ManagerSound.PlaySound(ReSouce.soundEatBullet);
                                //neu dang duoc bao ve thi reset lai thoi gian dem bao ve
                                TimeCountProtected = 0;
                                Isprotected = true;
                                break;
                            default:
                                GameplayScreen.numcoin++;
                                GameplayScreen.numScore += 100;
                                ManagerSound.PlaySound(ReSouce.soundEatCoin);
                                break;
                        }
                    }
                }


                //va chm voi dan quan dich
                if (b.typeobject == OBJECTTYPE.BULLET && !Isprotected)
                {
                    Bullet en = (Bullet)b;

                    if (this.bound.Intersects(b.bound) && en.bulletType == BULLETTYPE.BULLETENEMY)
                    {
                        b.isvisible = false;
                        Player.PowerBullet = (Player.PowerBullet + 1) / 2;
                        listexplore.Add(new Explore(EXPLOSIONTYPE.PLAYEREXPLOSION, postion));
                        this.isvisible = false;
                        GameplayScreen.life--;
                        TimeCountProtected = 0;
                        Isprotected = false;
                        ManagerSound.PlaySound(ReSouce.soundPlayerExplosion);
                    }
                }
                if (b.typeobject == OBJECTTYPE.ENEMY && !Isprotected)
                {
                    if (this.bound.Intersects(b.bound))
                    {
                        Player.PowerBullet = (Player.PowerBullet + 1) / 2;
                        listexplore.Add(new Explore(EXPLOSIONTYPE.PLAYEREXPLOSION, postion));
                        this.isvisible = false;
                        GameplayScreen.life--;
                        //2 dong nay de tranh nhug chet dang con bao ve
                        TimeCountProtected = 0;
                        Isprotected = false;
                        ManagerSound.PlaySound(ReSouce.soundPlayerExplosion);
                    }
                }
                //update bullet of player collision
                for (int i = 0; i < Listbullet.Count; i++)
                {
                    Listbullet[i].UpdateCollion(b);

                }
            }
        }
        public void Shoot()
        {
            Bullet bl;
            #region Dan xanh
            if (bullettype == BULLETTYPE.BULLETBLUE)
            {
                switch (PowerBullet)
                {
                    case 1:
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y));
                        Listbullet.Add(bl);
                        StrenghtBullet = 2;
                        break;
                    case 2:
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 8, postion.Y));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 8, postion.Y));
                        Listbullet.Add(bl);
                        StrenghtBullet = 1;
                        break;
                    case 3:
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 15, postion.Y));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 15, postion.Y));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y - 10));
                        Listbullet.Add(bl);
                        StrenghtBullet = 1;
                        break;
                    case 4:
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 15, postion.Y), -0.5f, new Vector2(-3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 15, postion.Y), 0.5f, new Vector2(3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), -0.5f, new Vector2(-1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), 0.5f, new Vector2(1.5f, -8));
                        Listbullet.Add(bl);
                        StrenghtBullet = 3;
                        break;
                    case 5:
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 15, postion.Y), -0.5f, new Vector2(-3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 15, postion.Y), 0.5f, new Vector2(3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), -0.5f, new Vector2(-1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), 0.5f, new Vector2(1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y));
                        Listbullet.Add(bl);
                        StrenghtBullet = 3;
                        break;
                    case 6:
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 15, postion.Y), -0.5f, new Vector2(-3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 15, postion.Y), 0.5f, new Vector2(3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), -0.5f, new Vector2(-1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), 0.5f, new Vector2(1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 5, postion.Y));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 5, postion.Y));
                        Listbullet.Add(bl);
                        StrenghtBullet = 4;
                        break;
                    case 7:
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 15, postion.Y), -0.5f, new Vector2(-3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 15, postion.Y), 0.5f, new Vector2(3, -8));
                        Listbullet.Add(bl);
                        //4 tia 2 ben
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), -0.5f, new Vector2(-1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), 0.5f, new Vector2(1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 10, postion.Y), -0.5f, new Vector2(-1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 10, postion.Y), 0.5f, new Vector2(1.5f, -8));
                        Listbullet.Add(bl);
                        //2 tia o giua
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 5, postion.Y));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 5, postion.Y));
                        Listbullet.Add(bl);
                        StrenghtBullet = 4;
                        break;
                    default:
                        //4 tia ngoai cung
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 15, postion.Y), -0.5f, new Vector2(-3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 15, postion.Y), 0.5f, new Vector2(3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 20, postion.Y), -0.5f, new Vector2(-3, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 20, postion.Y), 0.5f, new Vector2(3, -8));
                        Listbullet.Add(bl);
                        //4 tia 2 ben
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), -0.5f, new Vector2(-1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X, postion.Y), 0.5f, new Vector2(1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 10, postion.Y), -0.5f, new Vector2(-1.5f, -8));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 10, postion.Y), 0.5f, new Vector2(1.5f, -8));
                        Listbullet.Add(bl);
                        //2 tia o giua
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X - 5, postion.Y));
                        Listbullet.Add(bl);
                        bl = new Bullet(BULLETTYPE.BULLETBLUE, new Vector2(postion.X + 5, postion.Y));
                        Listbullet.Add(bl);
                        StrenghtBullet = 5;
                        break;
                }
            }
            #endregion
            #region dan do
            else
                if (bullettype == BULLETTYPE.BULLETRED)
                {
                    switch (PowerBullet)
                    {
                        case 1:
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X, postion.Y), 1);
                            Listbullet.Add(bl);
                            StrenghtBullet = 1;
                            break;
                        case 2:
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X - 8, postion.Y), 1);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X + 8, postion.Y), 1);
                            Listbullet.Add(bl);
                            StrenghtBullet = 1;
                            break;
                        case 3:
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X - 15, postion.Y), 1);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X + 15, postion.Y), 1);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X, postion.Y - 10), 1);
                            Listbullet.Add(bl);
                            StrenghtBullet = 1;
                            break;
                        case 4:
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X - 20, postion.Y), 1);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X + 20, postion.Y), 1);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X, postion.Y - 10), 2);
                            Listbullet.Add(bl);
                            StrenghtBullet = 2;
                            break;
                        case 5:
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X - 15, postion.Y), 2);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X + 15, postion.Y), 2);
                            Listbullet.Add(bl);
                            StrenghtBullet = 4;
                            break;
                        case 6:
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X - 20, postion.Y), 2);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X + 20, postion.Y), 2);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X, postion.Y - 15), 1);
                            Listbullet.Add(bl);
                            StrenghtBullet = 3;
                            break;
                        case 7:
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X - 20, postion.Y), 2);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X + 20, postion.Y), 2);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X, postion.Y - 15), 2);
                            Listbullet.Add(bl);
                            StrenghtBullet = 4;
                            break;
                        default:
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X - 20, postion.Y), 2);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X + 20, postion.Y), 2);
                            Listbullet.Add(bl);
                            bl = new Bullet(BULLETTYPE.BULLETRED, new Vector2(postion.X, postion.Y - 15), 3);
                            Listbullet.Add(bl);
                            StrenghtBullet = 5;
                            break;
                    }
                }

            #endregion
        }
        public void RemoveAllList()
        {

            for (int i = 0; i < listexplore.Count; i++)
            {
                if (!listexplore[i].isvisible)
                {
                    listexplore.RemoveAt(i);
                    i--;
                }
            }
            for (int i = 0; i < Listbullet.Count; i++)
            {
                if (!Listbullet[i].isvisible)
                {
                    Listbullet.RemoveAt(i);
                    i--;
                }
            }
            for (int i = 0; i < ListBoom.Count; i++)
            {
                if (!ListBoom[i].islife)
                {
                    ListBoom.RemoveAt(i);
                    i--;
                }
            }

        }

    }
}
